<?php


namespace app\system\controller\cms;

use app\common\model\cms\ClassModel;
use app\common\model\cms\TableModel;
use app\system\controller\BaseController;

class ClassController extends BaseController
{

  /**
   * 不检测权限方法
   * @var array
   */
  protected $noCheck = [];

  /**
   * 权限映射
   * @var array
   */
  protected $mapCheck = [
      '*' => 'index',
  ];

  /**
   * 当前模型
   * @var null
   */
  protected $model = null;

  /**
   * 初始化
   */
  public function __construct()
  {
    parent::__construct();
    $this->model = new ClassModel();
  }

  /**
   * 列表
   * @return array|string
   * @throws \Exception
   */
  public function index()
  {
    $tree = $this->model->getList(true);
    $this->assign('tree', $tree);
    return $this->fetch();
  }

  /**
   * 获取分类数据
   * @return mixed
   * @throws \Exception
   */
  public function tree()
  {
    $tree = $this->model->getList();
    $tree = array_values($tree);
    return json($tree);
  }

  /**
   * 添加
   * @return string
   * @throws \Exception
   */
  public function add()
  {
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'name|分类名称' => 'require',
      ]);
      if($data['class_type']==2 && empty($data['class_url'])) abort(500,'外链分类必须填写外链地址');
      $this->model->add($data);
      $this->success('操作成功');
    }
    $pid       = $this->request->param('pid/d');
    $tree      = $this->model->getList();
    $table     = TableModel::order('show_order asc')->select();
    $tableName = isset($tree[$pid]['table_name']) ? $tree[$pid]['table_name'] : '';
    $this->assign('tree', $tree);
    $this->assign('table', $table);
    $this->assign('table_name', $tableName);
    return $this->fetch();
  }

  /**
   * 编辑
   * @return string
   * @throws \Exception
   */
  public function edit()
  {
    if ($this->request->isAjax()) {
      $data = $this->request->param();
      $this->validate($data, [
          'name|分类名称' => 'require',
      ]);
      if($data['class_type']==2 && empty($data['class_url'])) abort(500,'外链分类必须填写外链地址');
      $this->model->edit($data);
      $this->success('操作成功');
    }
    $id    = $this->request->param('id/d');
    $info  = $this->model->where('id', $id)->find();
    $tree  = $this->model->getList();
    $tree  = format_tree_select($tree, $tree[$id]['path']);
    $table = TableModel::order('show_order asc')->select();
    $this->assign('tree', $tree);
    $this->assign('info', $info);
    $this->assign('table', $table);
    return $this->fetch();
  }

  /**
   * editable
   * @return string
   */
  public function editable()
  {
    try {
      $data = $this->request->param();
      $this->validate($data, [
          'pk|标识' => 'number',
          'name|键' => 'require',
          'value|值' => 'require',
      ]);
      $this->model->where('id', $data['pk'])->update([$data['name'] => $data['value']]);
    } catch (\Throwable $th) {
      return response($th->getMessage(), 500);
    }
    return 'ok';
  }

  /**
   * 设置状态
   */
  public function set()
  {
    $id     = $this->request->param('id/d');
    $info   = $this->model->where('id', $id)->find();
    $isShow = $info['is_show'] == 1 ? 0 : 1;
    $this->model->where('id', $id)->update(['is_show' => $isShow]);
    $this->success('操作成功');
  }

  /**
   * 刷新
   * @throws \Exception
   */
  public function refresh()
  {
    $this->model->getList(true);
    $this->success('操作成功');
  }

  /**
   * 删除分类
   * @throws \Exception
   */
  public function del()
  {
    $id = $this->request->param('id/d');
    $this->model->del($id);
    $this->success(' 操作成功');
  }

}